package com.example.student.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.student.generated.entity.Course;
import com.example.student.generated.entity.Student;
import com.example.student.service.MyCourseService;
import com.exmaple.result.PageReq;
import com.exmaple.result.PageReqUnderCondition;
import com.exmaple.result.PageRes;
import com.exmaple.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.exmaple.result.ErrorCode.*;

@RestController
@RequestMapping(value = "/course")
public class CourseController {
    @Resource
    private MyCourseService myCourseService;

    /**
     * 根据课程ID查找
     * @param id
     * @return (success)    、(failure)空
     */
    @GetMapping("/{id}")
    public Result<Course> getId(@PathVariable Long id)
    {
        Course course = myCourseService.getById(id);
        return course!=null? Result.success(course):Result.error(USER_ERROR_A0402);
    }

    /**
     * 根据课程姓名查找多个
     * @param name
     * @return (success)    、(failure)空
     */
    @GetMapping("/some")
    public Result<List<Course>> getSome(@RequestParam String name)
    {
        List<Course> courseList = myCourseService.lambdaQuery().like(StringUtils.isNotBlank(name), Course::getCourseName, name).list();
        return courseList.isEmpty()? Result.error(USER_ERROR_A0201): Result.success(courseList);
    }

    /**
     * 查找全部课程
     * @return 全部课程
     */
    @GetMapping("/all")
    public Result<List<Course>> getAll()
    {
        List<Course> courseList = myCourseService.list();
        return courseList.isEmpty()? Result.error(USER_ERROR_A0201): Result.success(courseList);
    }

    /**
     * 根据课程姓名分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public PageRes<Course> getPage(@RequestParam(value = "pageNo") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize,@RequestParam(required = false) String name)
    {
        List<Course> courseList= new ArrayList<>();
        //无条件分页查询
        if (name.isEmpty())
        {
            PageReq pageReq=new PageReq();
            pageReq.setPageNum(pageNum);
            pageReq.setPageSize(pageSize);
            Page<Course> coursePage = new Page<>(pageReq.getPageNum(), pageReq.getPageSize());
            courseList = myCourseService.page(coursePage).getRecords();
        }
        //根据学生姓名分页查询
        else
        {
            PageReqUnderCondition pageReq=new PageReqUnderCondition();
            pageReq.setPageNum(pageNum);
            pageReq.setPageSize(pageSize);
            pageReq.setCondition1(name);
            Page<Course> coursePage = new Page<>(pageReq.getPageNum(), pageReq.getPageSize());
            courseList = myCourseService.lambdaQuery()
                    .like(StringUtils.isNotBlank(pageReq.getCondition1()),Course::getCourseName,pageReq.getCondition1())
                    .page(coursePage)
                    .getRecords();
        }
        return courseList.isEmpty()? PageRes.error(USER_ERROR_A0201):PageRes.success(Integer.toUnsignedLong(courseList.size()),courseList);
    }

    /**
     * 创建单个课程记录
     * @param course
     * @return  (success)添加成功、(failure)添加失败
     */
    @PostMapping("/one")
    public Result<String> postOne(@RequestBody Course course)
    {
        if (myCourseService.getById(course.getId())==null&&myCourseService.lambdaQuery().eq(Course::getCourseCode,course.getCourseCode()).list()!=null)
        {
            myCourseService.save(course);
            return Result.success();
        }
        else
        {
            return Result.error(USER_ERROR_0001);
        }

    }

    /**
     * 创建多个课程记录
     * @param CourseList
     * @return  (success)添加成功、(failure)返回添加失败的课程 id
     */
    @PostMapping("/batch")
    public Result<String> postBatch(@RequestBody List<Course> CourseList)
    {
        StringBuilder res=new StringBuilder();
        for (Course course : CourseList)
        {
            if (myCourseService.lambdaQuery().eq(Course::getId,course.getId()).or().eq(Course::getCourseCode,course.getCourseCode()).list().isEmpty())
            {
                myCourseService.save(course);
            }
            else
            {
                res.append(course.getId()).append(" ");
            }
        }
        String s = res.toString();
        return s.isEmpty()?Result.error(USER_ERROR_0001):Result.success(s);
    }

    /**
     * 修改单个课程的信息
     * @param course
     * @return (success)修改成功 ， (failure)修改失败
     */
    @PutMapping("/one")
    public Result<String> putOne(@RequestBody Course course)
    {
        return myCourseService.lambdaUpdate().eq(Course::getId,course.getId()).update(course) ?Result.success():Result.error(USER_ERROR_0001);
    }

    /**
     * 批量修改课程的全部信息
     * @param CourseList
     * @return 返回修改成功的课程id
     */
    @PutMapping("/batch")
    public Result<String> putBatch(@RequestBody List<Course> CourseList)
    {
        StringBuilder res=new StringBuilder();
        for (Course course : CourseList) {
            boolean flag = myCourseService.lambdaUpdate().eq(Course::getId,course.getId()).update(course);
            if (flag)
            {
                res.append(course.getId());
                res.append(" ");
            }
        }
        String s=res.toString();
        return s.isEmpty()?Result.error(USER_ERROR_0001):Result.success(s);
    }

    /**
     * 删除课程以及其选课记录
     * @param id
     * @return (success)删除成功 (failure)删除失败
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteId(@PathVariable Long id)
    {
        return myCourseService.deleteOne(id)?Result.success():Result.error(USER_ERROR_0001);
    }

    /**
     * 批量删除课程以及其选课记录
     * @param CourseList
     * @return (failure)返回删除成功的课程Id (success)空字符串
     */
    @DeleteMapping("/batch")
    public Result<String> deleteBatch(@RequestBody List<Course> CourseList)
    {
        String s = myCourseService.deleteBatch(CourseList);
        return s.isEmpty()?Result.error(USER_ERROR_0001):Result.success(s);
    }
}
